package com.taotao.portal.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.taotao.common.utils.DateUtils;
import com.taotao.order.pojo.Order;
import com.taotao.pojo.TbUser;
import com.taotao.portal.pojo.CartItem;
import com.taotao.portal.service.CartService;
import com.taotao.portal.service.OrderService;

@Controller
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private CartService cartService;
	@Autowired
	private OrderService orderService;
	
	@RequestMapping("/order-cart")
	public String orderCart(HttpServletRequest request){
		List<CartItem> cartList = cartService.getCart(request);
		request.setAttribute("cartList", cartList);
		return "order-cart";
	}
	
	@RequestMapping("/create")
	public String createOrder(Order order,Model model,HttpServletRequest request, HttpServletResponse response){
		Order order2=null;
		try {
			TbUser user = (TbUser) request.getAttribute("user");
			order.setUserId(user.getId());
			order2 = orderService.createOrder(order,request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			model.addAttribute("message", e.getMessage());
			return "/error/exception";
			
		}
		model.addAttribute("orderId", order2.getOrderId());
		model.addAttribute("payment", order2.getPayment());
		Date date = DateUtils.computeDay(new Date(), 3);
		model.addAttribute("date", DateUtils.formatDate(date));
		return "success";
	}
}
